/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.managecontrollers;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.QuizDao;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Quiz;

/**
 * This controller manages the add,edit and remove for quizzes. redirects to the
 * add/edit page for quiz. also used for deleting a quiz.
 *
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class ManageQuizController extends HttpServlet {

    /**
     * This do get method checks if a quiz has to be removed,edited or added.
     * Calls the removeQuiz to remove, editQuiz to edit and manage Quiz to add
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("idremove") != null) {
            removeQuiz(Long.parseLong(request.getParameter("idremove")), request, response);
        } else if (request.getParameter("idedit") != null) {
            editQuiz(Long.parseLong(request.getParameter("idedit")), request, response);
        } else {
            manageQuiz(request, response);
        }
    }

    /**
     * Saves or edits the quiz after posting the form from the quiz. Redirects
     * to the manage quiz page after
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //Gets the values from the form
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        long courseid = Long.parseLong(request.getParameter("courseid"));
        //Sets time to 10(not used in application)
        long time = 10;

        //Makes a connection with the database using Dao's
        HibernateUtil hu = new HibernateUtil();
        CourseDao cd = new CourseDao(hu.openSession());
        QuizDao qd = new QuizDao(hu.openSession());

        if (request.getParameter("formoption").equals("Quizedit")) {// checks if its to add or edit a quiz
            Quiz quiz = qd.get(Long.parseLong(request.getParameter("quizid")));//gets the quiz

            quiz.setName(name);
            quiz.setTime(time);
            quiz.setDescription(description);

            qd.update(quiz);
        } else {//makes new quiz
            Quiz quiz = new Quiz(0, name, description, time, cd.get(courseid));

            qd.save(quiz);
        }



        hu.close();
        response.sendRedirect("Quiz");
    }

    /*
     * method to edit a quiz 
     * This method returns the quiz values to the form.
     * after it redirects to the add edit quiz page
     */
    private void editQuiz(long id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Makes a connection with the database using the Dao's
        HibernateUtil hu = new HibernateUtil();
        QuizDao qd = new QuizDao(hu.openSession());
        CourseDao cd = new CourseDao(hu.openSession());

        //gets the quiz to edit and sets it
        Quiz quiz = qd.get(id);
        request.setAttribute("Quizedit", quiz);
        //gets the course and sets it.
        Course course = cd.get(quiz.getCourse().getId());
        request.setAttribute("course", course);

        hu.close();

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Quiz", "Manage quizzes"),
            new Breadcrumbs("", "Edit quiz")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_quiz.jsp");
        dispatcher.forward(request, response);
    }

    /*
     * remove the quiz and redirects to the manage quiz page
     */
    private void removeQuiz(long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //opens hibernate connection trought session
        HibernateUtil hu = new HibernateUtil();
        QuizDao qd = new QuizDao(hu.openSession());

        //deletes the quiz
        qd.delete(qd.get(id));
        hu.close();
        response.sendRedirect("Quiz");
    }

    /*
     * This method sets a course.
     * Redirects to the add edit page to add a quiz
     */
    private void manageQuiz(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //gets the course id
        long courseid = Long.parseLong(request.getParameter("courseid"));

        //opens hibernate connection using Dao
        HibernateUtil hu = new HibernateUtil();
        CourseDao cd = new CourseDao(hu.openSession());
        Course course = cd.get(courseid);

        request.setAttribute("course", course);

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Quiz", "Manage quizzes"),
            new Breadcrumbs("", "Add quiz")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_quiz.jsp");
        dispatcher.forward(request, response);
    }
}
